package com.topisv.tms.utils.printModel;

import java.util.List;

import com.topisv.tms.service.vo.PackItems;
import com.topisv.tms.service.vo.PackStyleModel;
import com.topisv.tms.utils.PackStyleTablesOSA;

public class CommPackStyleModel {
	
	/**
	 * 替换
	 * 
	 * @param content
	 * @return
	 */
	public static String[] getBodyContentString(String content) {
		content = content.replaceAll("<[^>]*?>(.*?)", "="); // 删除左部
		content = content.replaceAll("(.*?)</[^>]*?>", "="); // 删除右部
		String[] bodyContents = content.split("=");
		return bodyContents;
	}

	
	public static String getCommBodyContent(String content,
			PackStyleModel packStyleModel) {
		List<PackItems> packItems = packStyleModel.getPackItems();
		StringBuffer tempBodyContentString = new StringBuffer();
		int number = 0;
		int allPrice = 0;
		for (int i = 0; i < packItems.size(); i++) {
			PackItems packItem = packItems.get(i);
			StringBuffer bodyContentString = new StringBuffer(
					"<tr style='height: 25px'>");
			String[] bodyContents = getBodyContentString(content);

			for (int l = 0; l < bodyContents.length; l++) {
				if (!bodyContents[l].equals("")) {
					if (bodyContents[l].equals("规格")) {
						bodyContentString.append("<td>"
								+ PackStyleTablesOSA.getContent(packItem
										.getFormat()) + "</td>");
					} else if (bodyContents[l].equals("商品数量")) {
						bodyContentString.append("<td>"
								+ PackStyleTablesOSA.getContent(packItem
										.getNumber()) + "</td>");
					} else if (bodyContents[l].equals("商品总数量") && number == 0) {
						number = 1;
						bodyContentString.append("<td rowspan='"
								+ packItems.size()
								+ "'>"

								+ PackStyleTablesOSA.getContent(packStyleModel
										.getNumber()) + "</td>");
					} else if (bodyContents[l].equals("品名")) {
						bodyContentString.append("<td>"
								+ packItem.getItemName() + "</td>");
					} else if (bodyContents[l].equals("折扣")) {
						bodyContentString.append("<td>"
								+ PackStyleTablesOSA.getContent(packItem
										.getDis()) + "</td>");
					} else if (bodyContents[l].equals("单价")) {
						bodyContentString.append("<td>"
								+ PackStyleTablesOSA.getContent(packItem
										.getPrice()) + "</td>");
					} else if (bodyContents[l].equals("订单总价") && allPrice == 0) {
						allPrice = 1;
						bodyContentString.append("<td rowspan='"
								+ packItems.size() + "'>"
								+ packStyleModel.getAllTotal() + "</td>");
					} else if (bodyContents[l].equals("应付金额")) {
						bodyContentString.append("<td>"
								+ PackStyleTablesOSA.getContent(packItem
										.getPay()) + "</td>");
					} else if (bodyContents[l].equals("实际金额")) {
						bodyContentString.append("<td >"
								+ PackStyleTablesOSA.getContent(packItem
										.getAmo()) + "</td>");
					} else if (bodyContents[l].equals("优惠金额")) {
						bodyContentString.append("<td >"
								+ packItem.getPromotion() + "</td>");
					} else if (bodyContents[l].equals("颜色")) {
						bodyContentString.append("<td >"
								+ PackStyleTablesOSA.getContent(packItem
										.getColor()) + "</td>");
					}
				}
			}
			bodyContentString.append("</tr>");
			tempBodyContentString.append(bodyContentString);
		}
		return tempBodyContentString.toString();
	}

	

	public static String getCommPackStyleContent(String content,
			PackStyleModel packStyleModel) {
		StringBuffer sb = new StringBuffer(
				PackStyleTablesOSA.commTopCommTable());
		sb.append(content + "</tr>");
		String bodyString = getCommBodyContent(content, packStyleModel);
		sb.append(bodyString);
		return sb.toString();
	}
}
